import AntModal from "@/components/pt/dialog/AntModal";

import {
  getBizTaizhang006,
  addBizTaizhang006,
  updateBizTaizhang006,
  findMaxSort,
} from "@/api/biz/bizTaizhang006";

export default {
  name: "CreateForm",
  props: {
    deptOptions: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      deptList: [],

      replaceFields: {
        children: "children",
        key: "id",
        value: "label",
      },
      total: 0,
      id: undefined,
      formTitle: "添加监督检查台账",
      // 表单参数
      form: { dateRange: [], tz00605: "整改中" },
      rules: {
        dateRange: [
          {
            required: true,
            message: "检查时间不能为空",
            trigger: ["change", "blur"],
          },
        ],
        tz00602: [{ required: true, message: "文号不能为空", trigger: "blur" }],
      },
    };
  },
  filters: {},
  created() {
    console.log("this.deptOptions", this.deptOptions);
  },
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    handleChange(value) {
      this.form.tz00605 = value;
    },

    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.deptList = [];
      this.dateRange = [];
      this.form = {
        id: undefined,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        companyId: undefined,

        tz00601: undefined,

        tz00602: undefined,

        tz00603: undefined,

        tz00604: undefined,

        tz00605: undefined,

        tz00606: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.form.tz00605 = "整改中";
        this.formTitle = "添加监督检查台账";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizTaizhang006Id = row.id;
      getBizTaizhang006(bizTaizhang006Id).then((response) => {
        this.form = response.data;
        this.form.dateRange = response.data.tz00601.split("~");
        this.deptList = response.data.tz00606.split("&");
        this.formTitle = "修改监督检查台账";
        this.spinning = !this.spinning;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.tz00601 =
            this.form.dateRange[0] + "~" + this.form.dateRange[1];
          this.form.tz00606 = this.deptList.join("&");
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizTaizhang006(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizTaizhang006(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/taizhang006/index";
      this.$router.push(index);
    },
  },
};
